package com.finley.flowabledemo.design.state.demo02.state;


import cn.hutool.core.util.StrUtil;
import com.finley.flowabledemo.design.state.demo02.source.*;

import java.util.Objects;

/**
 * @description:
 * @author: xianfei.chao
 * @date 2025/6/24 17:22
 * @version: 1.0
 */
public class ShippedStatusState extends OrderStatusState{


    @Override
    void handle(OrderStatusContext orderStatusContext) {
        DatabaseConnection2 conn = orderStatusContext.getConn();
        Order order = orderStatusContext.getOrder();
        if (StrUtil.equalsIgnoreCase(order.getStatus(), OrderStatus.PAID.getCode())) {
            ShippingService shippingService = new ShippingService();
            shippingService.createShipment(order);
            conn.executeUpdate( "UPDATE orders SET status='SHIPPED' WHERE id=" + order.getId());
            EmailSender emailSender = orderStatusContext.getEmailSender();
            emailSender.sendOrderConfirmation(order.getCustomerEmail(),  "订单发货",  "您的订单#" + order.getId() +  "已发货");
        }else{
            if (Objects.nonNull(conn)) {
                conn.close();
            }
        }
    }
}
